# encoding: utf-8
import os

from sqlalchemy import distinct

from app.extensions import db
from app.modules.integration.models import IntegrationPlan
from app.modules.projectPlan.models import PlanMember


def uploadFile(path, file_name, file):
    """
    保存上传文件
    :param path: 文件保存目录
    :param file_name: 保存文件名
    :param file: 文件对象
    :return:
    """
    if not os.path.exists(path):
        os.makedirs(path)

    _file_path = os.path.join(path, file_name)

    if os.path.exists(_file_path):
        os.remove(_file_path)

    file.save(_file_path)
    return _file_path

def getIntegrationMembers(_integration_id):
    _list = []
    if _integration_id is None:
        return _list

    _planids = []
    for _plan in IntegrationPlan.query.filter_by(integration_id = _integration_id).all():
        _planids.append(_plan.plan_id)

    for account, realname in db.session.query(distinct(PlanMember.account), PlanMember.realname).filter(
            PlanMember.plan_id.in_(_planids)).all():
        _list.append({'account': account, 'realname': realname,'role':'member'})
    return _list